use eframe::egui::{self, Ui};
use chrono::prelude::*;
use crate::app::{App, state::AppEvent};
use crate::ui::theme::AppTheme;
use crate::ui::widgets::{make_button, card_frame};

pub struct SpeedTestPanel;

impl SpeedTestPanel {
    pub fn render(ui: &mut Ui, app: &App, theme: &AppTheme) -> Option<AppEvent> {
        let mut event = None;
        
        // 标题
        ui.vertical_centered(|ui| {
            ui.add_space(16.0);
            ui.heading("服务器测速");
            ui.add_space(16.0);
        });
        
        // 测速控制按钮
        ui.horizontal(|ui| {
            let test_btn = make_button(
                ui,
                "测试全部服务器",
                theme.primary,
                [180.0, 36.0],
                16.0,
                6.0
            );
            
            if test_btn.clicked() {
                event = Some(AppEvent::SpeedTest);
            }
            
            ui.add_space(16.0);
            
            let clear_btn = make_button(
                ui,
                "清除测速结果",
                theme.error,
                [180.0, 36.0],
                16.0,
                6.0
            );
            
            if clear_btn.clicked() {
                event = Some(AppEvent::ClearSpeedTest);
            }
        });
        
        ui.add_space(16.0);
        
        // 测速结果表格
        card_frame(theme, false).show(ui, |ui| {
            ui.vertical(|ui| {
                ui.heading("测速结果");
                ui.add_space(8.0);
                
                // 表格头部
                ui.horizontal(|ui| {
                    ui.label(egui::RichText::new("服务器").strong());
                    ui.with_layout(
                        egui::Layout::right_to_left(egui::Align::Center),
                        |ui| {
                            ui.label(egui::RichText::new("延迟").strong());
                        }
                    );
                });
                
                ui.separator();
                
                // 表格内容
                egui::ScrollArea::vertical()
                    .max_height(300.0)
                    .show(ui, |ui| {
                        if app.speed_test_results.is_empty() {
                            ui.vertical_centered(|ui| {
                                ui.add_space(32.0);
                                ui.label("暂无测速数据");
                                ui.add_space(32.0);
                            });
                        } else {
                            // 显示测速结果
                            for result in &app.speed_test_results {
                                ui.horizontal(|ui| {
                                    ui.label(&result.server_name);
                                    
                                    ui.with_layout(
                                        egui::Layout::right_to_left(egui::Align::Center),
                                        |ui| {
                                            let ping_text = if result.ping < 0.0 {
                                                "超时".to_string()
                                            } else {
                                                format!("{:.0} ms", result.ping)
                                            };
                                            
                                            // 根据延迟显示不同颜色
                                            let ping_color = if result.ping < 0.0 {
                                                theme.error
                                            } else if result.ping < 100.0 {
                                                theme.success
                                            } else if result.ping < 200.0 {
                                                theme.warning
                                            } else {
                                                theme.error
                                            };
                                            
                                            ui.label(
                                                egui::RichText::new(ping_text)
                                                    .color(ping_color)
                                                    .strong()
                                            );
                                        }
                                    );
                                });
                                
                                ui.add_space(4.0);
                            }
                        }
                    });
            });
        });
        
        event
    }
} 